﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace prjYan.Forum.Controllers
{
    public class CommentController : BaseController
    {
        private static int PageSize = 5;
        //
        // GET: /Comment/

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult AjaxLoadComment(int contentId, int p = 1)
        {
            var model = YanForum.Engine.Services.Comment.GetListCommentByContentId(contentId, PageSize, p);
            model.ContentId = contentId;
            model.PageSize = PageSize;
            model.PageIndex = p;
            return PartialView(model);
        }

        public PartialViewResult AjaxLoadSubComment(int commentId)
        {
            var model = YanForum.Engine.Services.Comment.GetListSubComment(commentId);
            return PartialView(model);
        }

        public JsonResult AjaxCheckAuthorize()
        {
            if (Request.IsAuthenticated)
                return Json(new { status = true});
            else
                return Json(new { status = false });
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AjaxSubmitComment(int commentId, string commentTxt, int contentId)
        {
            try
            {
                if (Request.IsAuthenticated)
                {
                    if (Session["WaitingComment"] != null)
                    {
                        DateTime WaitingComment = (DateTime)Session["WaitingComment"];
                        if (WaitingComment > DateTime.Now)
                        {
                            return Json(new { status = false, message = "Bạn comment nhanh quá! Chờ xíu đã!" });
                        }
                    }

                    if (commentTxt.Length > 2000)
                    {
                        return Json(new { status = false, message = "Comment của bạn quá dài, vui lòng xem lại!"});
                    }

                    Session["WaitingComment"] = DateTime.Now.AddSeconds(20);
                    YanForum.Engine.Services.Comment.InsertComment(GetUserId, commentId, contentId, commentTxt);

                    return Json(new { status = true});
                }
                return Json(new { status = false, message = "Vui lòng đăng nhập để comment!" });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AjaxDeleteComment(string commentId)
        {
            try
            {
                if (Request.IsAuthenticated && User.IsInRole(YanForum.Engine.ViewModels.UserRoles.Admin))
                {
                    YanForum.Engine.Services.Comment.CommentDelete(commentId,GetUserId);
                    return Json(new { status = true });
                }
                else
                    return Json(new { status = false });
            }
            catch (Exception ex)
            {
                return Json(new { status = false});
            }
        }
    }
}
